using System;
using System.Collections.Generic;

using IBatisNet.Common;
using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about expense type
    /// </summary>
    public class ExpenseTypeDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ExpenseTypeDAO));

        /// <summary>
        /// Method responsible by getting information about expense type in according to a given description
        /// </summary>
        /// <param name="String">expense type's description</param>
        /// <returns>ExpenseType - user value object matching with given description</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public ExpenseType findByDescription(String description)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // expense type value object to be returned
            ExpenseType expenseTypeResult = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                expenseTypeResult = ibatisMapper.QueryForObject<ExpenseType>("ExpenseType.findByDescription", description);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving expense type information", ex);
                throw new PersistenceException("Error while retrieving expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeResult;
        }

        /// <summary>
        /// Method responsible by getting information all active expense types
        /// </summary>
        /// <returns>IList - list of expense type </returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<ExpenseType> findAllActive()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // expense type value object list to be returned
            IList<ExpenseType> expenseTypeList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                expenseTypeList = ibatisMapper.QueryForList<ExpenseType>("ExpenseType.findAllActive", null);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving expense type information", ex);
                throw new PersistenceException("Error while retrieving expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeList;
        }

        /// <summary>
        /// Method responsible by adding a new expense type to database
        /// </summary>
        /// <param name="ExpenseType">expense type information to be saved</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void create(ExpenseType expenseTypeVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Insert("ExpenseType.insert", expenseTypeVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while saving expense type information", ex);
                throw new PersistenceException("Error while saving expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by updating an expense type to database
        /// </summary>
        /// <param name="ExpenseType">expense type information to be saved</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void update(ExpenseType expenseTypeVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Update("ExpenseType.update", expenseTypeVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while saving expense type information", ex);
                throw new PersistenceException("Error while saving expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by finding expense type using any of its properties
        /// </summary>
        /// <param name="ExpenseType">expense type vo filter</param>
        /// <returns>IList - list of expense type retrieved in according to filter</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<ExpenseType> findByAny(ExpenseType expenseTypeVOFilter)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of modalities found
            IList<ExpenseType> expenseTypeList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                expenseTypeList = ibatisMapper.QueryForList<ExpenseType>("ExpenseType.findByAny", expenseTypeVOFilter);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving expense type information", ex);
                throw new PersistenceException("Error while retrieving expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeList;
        }

        /// <summary>
        /// Method responsible by removing an expense type from database
        /// </summary>
        /// <param name="Int32">expense type identifier</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void remove(Int32 expenseTypeId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Delete("ExpenseType.delete", expenseTypeId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while removing expense type information", ex);
                throw new PersistenceException("Error while removing expense type information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

    }
}
